package com.ruoyi.project.orderSystem.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.orderSystem.domain.OrderStation;
import com.ruoyi.project.orderSystem.domain.StationItemFile;
import com.ruoyi.project.orderSystem.domain.WorkOrder;
import com.ruoyi.project.orderSystem.domain.vo.OrderStationItemVO;
import com.ruoyi.project.orderSystem.mapper.OrderStationMapper;
import com.ruoyi.project.orderSystem.mapper.StationItemFileMapper;
import com.ruoyi.project.orderSystem.mapper.WorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.orderSystem.mapper.OrderStationItemMapper;
import com.ruoyi.project.orderSystem.domain.OrderStationItem;
import com.ruoyi.project.orderSystem.service.IOrderStationItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单站点巡检项Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-24
 */
@Service
public class OrderStationItemServiceImpl implements IOrderStationItemService 
{
    @Autowired
    private OrderStationItemMapper orderStationItemMapper;
    @Autowired
    private StationItemFileMapper stationItemFileMapper;
    @Autowired
    private OrderStationMapper orderStationMapper;
    @Autowired
    private WorkOrderMapper workOrderMapper;

    /**
     * 查询工单站点巡检项
     * 
     * @param id 工单站点巡检项ID
     * @return 工单站点巡检项
     */
    @Override
    public OrderStationItem selectOrderStationItemById(Long id)
    {
        return orderStationItemMapper.selectOrderStationItemById(id);
    }

    /**
     * 查询工单站点巡检项列表
     * 
     * @param orderStationItem 工单站点巡检项
     * @return 工单站点巡检项
     */
    @Override
    public List<OrderStationItem> selectOrderStationItemList(OrderStationItem orderStationItem)
    {
        List<OrderStationItem> orderStationItems = orderStationItemMapper.selectOrderStationItemList(orderStationItem);
        orderStationItems.forEach(x->{
            StationItemFile stationItemFile = new StationItemFile();
            stationItemFile.setStationItemId(x.getId());
            List<StationItemFile> stationItemFiles = stationItemFileMapper.selectStationItemFileList(stationItemFile);
            x.setStationItemFileList(stationItemFiles);
        });
        return orderStationItems;
    }

    /**
     * 新增工单站点巡检项
     * 
     * @param orderStationItem 工单站点巡检项
     * @return 结果
     */
    @Override
    public int insertOrderStationItem(OrderStationItem orderStationItem)
    {
        return orderStationItemMapper.insertOrderStationItem(orderStationItem);
    }

    /**
     * 修改工单站点巡检项
     * 
     * @param orderStationItem 工单站点巡检项
     * @return 结果
     */
    @Override
    public int updateOrderStationItem(OrderStationItem orderStationItem)
    {
        return orderStationItemMapper.updateOrderStationItem(orderStationItem);
    }

    /**
     * 批量删除工单站点巡检项
     * 
     * @param ids 需要删除的工单站点巡检项ID
     * @return 结果
     */
    @Override
    public int deleteOrderStationItemByIds(Long[] ids)
    {
        return orderStationItemMapper.deleteOrderStationItemByIds(ids);
    }

    /**
     * 删除工单站点巡检项信息
     * 
     * @param id 工单站点巡检项ID
     * @return 结果
     */
    @Override
    public int deleteOrderStationItemById(Long id)
    {
        return orderStationItemMapper.deleteOrderStationItemById(id);
    }

    @Override
    @Transactional
    public int saveOrderItem(OrderStationItem orderStationItem) {
        Optional.ofNullable(orderStationItem.getOrderStationId()).orElseThrow(()->new BaseException("参数缺失"));
//        if(orderStationItem.getItems()==null||orderStationItem.getItems().length==0){
//            throw new BaseException("巡检事项不能退为空");
//        }
        try {
//            List<OrderStationItem> orderStationItems = new ArrayList<>();
//            Arrays.stream(orderStationItem.getItems()).forEach(x->{
//                OrderStationItem orderStationItem1 = new OrderStationItem();
//                orderStationItem1.setOperatorId(SecurityUtils.getLoginUser().getUser().getUserId());
//                orderStationItem1.setGmtOperate(new Date());
//                orderStationItem1.setItem(x);
//                orderStationItem1.setOrderStationId(orderStationItem.getOrderStationId());
//                orderStationItems.add(orderStationItem1);
//            });
//            int i = orderStationItemMapper.batchInsert(orderStationItems);
//            return i;
            //判断之前是发有巡检项
            OrderStationItem orderStationItem1 = new OrderStationItem();
            orderStationItem1.setOrderStationId(orderStationItem.getOrderStationId());
            List<OrderStationItem> orderStationItems = orderStationItemMapper.selectOrderStationItemList(orderStationItem1);
            if(orderStationItems.size()>0){
                List<Long> collect = orderStationItems.stream().map(OrderStationItem::getId).collect(Collectors.toList());
                Long[] longs = new Long[collect.size()];
                collect.toArray(longs);
                orderStationItemMapper.deleteOrderStationItemByIds(longs);
            }
            Long orderStationId = orderStationItem.getOrderStationId();
            OrderStation orderStation = new OrderStation();
            orderStation.setId(orderStationId);
            orderStation.setStatus(1);
            orderStationMapper.updateOrderStation(orderStation);
            int i = orderStationItemMapper.insertOrderStationItem(orderStationItem);
            //报存图片
            if(orderStationItem.getStationItemFiles()!=null&&orderStationItem.getStationItemFiles().size()>0){
                //删除之前的图片
                int z =stationItemFileMapper.deleteStationItemFileByStationItemId(orderStationItem.getId());
                List<StationItemFile> stationItemFileList = new ArrayList<>();
                orderStationItem.getStationItemFiles().forEach(x->{
                    StationItemFile stationItemFile = new StationItemFile();
                    stationItemFile.setFileUrl(x);
                    stationItemFile.setStationItemId(orderStationItem.getId());
                    stationItemFileList.add(stationItemFile);
                });
                int y = stationItemFileMapper.batchInsert(stationItemFileList);
            }
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("新增事项失败");
        }
    }

    @Override
    public OrderStationItemVO getOrderStationItemByOrderStationId(Long orderStationId) {
        OrderStationItemVO orderStationItemVO = orderStationItemMapper.getOneByOrderStationId(orderStationId);
        if(orderStationItemVO.getId()!=null){
            //获取图片
            StationItemFile stationItemFile = new StationItemFile();
            stationItemFile.setStationItemId(orderStationItemVO.getId());
            List<StationItemFile> stationItemFiles = stationItemFileMapper.selectStationItemFileList(stationItemFile);
//            orderStationItemVO.setStationItemFileList(stationItemFiles);
//            WorkOrder workOrder = workOrderMapper.selectWorkOrderById(orderStationItemVO.getOrderId());
//            if(OrderStatus.ORDER_REJECT.getKey()==workOrder.getOrderStatus().intValue()||OrderStatus.ORDER_IN_FILE.getKey()==workOrder.getOrderStatus().intValue()){
//                orderStationItemVO.setStationItemFileList(stationItemFiles);
//            }else {
//                List<String> collect = stationItemFiles.stream().map(StationItemFile::getFileUrl).collect(Collectors.toList());
//                orderStationItemVO.setStationItemFiles(collect);
//            }
            List<String> collect = stationItemFiles.stream().map(StationItemFile::getFileUrl).collect(Collectors.toList());
            orderStationItemVO.setStationItemFiles(collect);
        }
        return orderStationItemVO;
    }
}
